﻿using System.Collections;
using System.Diagnostics.CodeAnalysis;
using Admin.CommonLib.Models.Permission;
using Admin.RazorLib.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Radzen;

namespace Admin.RazorLib.Pages;

public partial class PermissionEdit
{
    private PermissionEditRequestDto _editRequestDto = new();
    protected IList<PermissionTreeResponseDto> TreeData = new List<PermissionTreeResponseDto>();

    [Parameter] public Guid? Key { get; set; }

    [Inject] public IPermissionService PermissionService { get; set; }

    [Inject] [NotNull] public DialogService DialogService { get; set; }

    async Task OnSubmit()
    {
        if (Key != null)
        {
            var result = await PermissionService.UpdatePermission(Key.Value, _editRequestDto);

            if (result.Succeeded)
            {
                DialogService.Close(null);
            }
        }
        else
        {
            var result = await PermissionService.CreatePermission(_editRequestDto);

            if (result.Succeeded)
            {
                DialogService.Close(null);
            }
        }
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        var result = await PermissionService.QueryPermissionsByTree(1);
        if (result.Succeeded)
        {
            TreeData = result.Data;
        }
    }

    protected override async Task OnParametersSetAsync()
    {
        _editRequestDto = new PermissionEditRequestDto();
        //_editRequestDto.ParentName = ParentName;
        //_editRequestDto.ParentId = ParentId;
        if (Key != null)
        {
            var result = await PermissionService.GetPermission(Key.Value);
            if (result.Succeeded && result.Data != null)
            {
                _editRequestDto.Code = result.Data.Code;
                _editRequestDto.Name = result.Data.Name;
                _editRequestDto.PermissionType = result.Data.PermissionType;
                _editRequestDto.ParentId = result.Data.ParentId;
                _editRequestDto.ParentName = result.Data.ParentName;
                _editRequestDto.Url = result.Data.Uri;
                _editRequestDto.Icon = result.Data.Icon;
                _editRequestDto.Sort = result.Data.Sort;
                _editRequestDto.Description = result.Data.Description;
            }
        }
    }

    protected void CancelButtonClick(MouseEventArgs args)
    {
        DialogService.Close(null);
    }


    object GetItemFromTreeHandler(IEnumerable data, object value)
    {
        PermissionTreeResponseDto? permissionTreeResponseDto = null;
        var permissionTreeResponseDtos = (IList<PermissionTreeResponseDto>)data;
        if (value != null)
        {
            var currentValue = (Guid)value;
            if (permissionTreeResponseDtos.Count > 0 && currentValue != Guid.Empty)
            {
                permissionTreeResponseDto = GetChildSelectedItem(permissionTreeResponseDtos, currentValue);
            }
        }
        return permissionTreeResponseDto!;
    }

    private PermissionTreeResponseDto? GetChildSelectedItem(
        IList<PermissionTreeResponseDto> treeResponseDtos, Guid value)
    {
        PermissionTreeResponseDto? item = null;
        foreach (var childItem in treeResponseDtos)
        {
            if (childItem.Children != null)
            {
                return GetChildSelectedItem(childItem.Children, value);
            }

            if (childItem.Key == value)
            {
                item = childItem;
                break;
            }
        }

        return item;
    }
}